package com.cty._01_Base._09_01_QueueWithTwoStacks;

import java.util.Stack;

/**
 * @Auther: cty
 * @Date: 2020/6/29 11:57
 * @Description: 面试题9：用两个栈实现一个队列
 * @version: 1.0
 */
public class QueueWithTwoStacks<T> {
    private Stack<T> stack1;
    private Stack<T> stack2;

    public QueueWithTwoStacks(){
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void appendTail(T node){
        stack1.push(node);
    }

    public T deleteHead(){
        if(!stack2.isEmpty())
            return stack2.pop();

        while(!stack1.isEmpty())
            stack2.push(stack1.pop());
        return stack2.pop();
    }

}  // end class
